<?php

namespace App\Console\Commands;

use App\Models\BookIsbn;
use App\Models\Cart;
use App\Models\Goods;
use App\Services\DoubanService;
use App\Services\GoodService;
use App\Services\GraspService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class TestCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'system:test {isbn?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $isbn = $this->argument('isbn');
//        $this->clearIsbn($isbn);
        // $id = "389357232032191711";
        // $a = GraspService::lockAddCart($id);
        // var_dump($a);
        // $lockRet = GraspService::lockDzyGood($id);
        // var_dump($lockRet);
        $this->addCart();

    }

    protected function addCart()
    {
        $goods = Cart::where('is_test_add', 0)->offset(0) ->limit(300)->get();
        foreach ($goods as $good) {
            $isbn = $good->isbn;
            if ($good->item_id == 0) {
                $dzyGood = GraspService::dzyIsbnGood($isbn);
                if (!$dzyGood || empty($dzyGood)) {
                    continue;
                }
                if (!isset($dzyGood['data'][0]['book'])) {
                    continue;
                }
                $book = $dzyGood['data'][0]['book'];
                $id = $book['id'];
                $price = $book['price'];
                $good->item_id = $id;
                $good->price = $price;
                $good->plan_remind = 1;
                $good->image = $book['images']['origin'];
            } else {
                $id = $good->item_id;
            }

            //加入购物车
            $res = GraspService::execAddDzyCart($id,'449272611965377734' , '_ga=GA1.2.1542314278.1606716676; Hm_lvt_046a9150e95f58699927e236d330965a=1616723250,1618455769,1618821048; _gid=GA1.2.1795265890.1618821049; _gat=1; Hm_lpvt_046a9150e95f58699927e236d330965a=1618901698; fish_c0=2|1:0|10:1618901696|7:fish_c0|24:NDQ5MjcyNjExOTY1Mzc3NzM0|badbacaeec7e2f2f799be5ff3b4b5642a8330626c3a17e01939b9fc8a3ff8530');
            if ($res) {
                $good->is_test_add = 1;
                $good->save();
                print "wdy加入购物车成功:" . $good['title'] . "\n";
                Log::info("账号wdy加入购物车成功", [$id]);
            }
        }
    }

    protected function clearIsbn($isbn)
    {
        $goods = Goods::where('isbn', '>', 3)->where('id', '>', '1000001')->get();
        $i = 0;
        foreach ($goods as $good) {
            if ($good['isbn'] > 3) {
                $bookisbn = BookIsbn::where('isbn10', $good['isbn'])->orWhere('isbn13', $good['isbn'])->first();
                if (is_null($bookisbn)) {
                    $douban = DoubanService::isbnBook($good['isbn']);
                    if (!$douban || isset($douban['code'])) {
                        continue;
                    }

                    GoodService::insertDoubanBook($douban);
                }

            }
            $i++;
            print "正在更新第：" . $i . "\n";
            //更新isbn数量
            GraspService::updateIsbnTable($good['isbn']);
        }

        // foreach ($goods as $good) {
        //     if (strlen($good['title']) > 150) {
        //         $good->isbn = 0;
        //         $good->save();
        //     }
        // }
        // $goods2 = Goods::selectRaw("id,count(isbn) as num,remark,isbn")->where('isbn', '9787020100446')->groupBy('title')->get();
        // foreach ($goods2 as $good2) {
        //     if ($good2['num'] == 1) {
        //         $good2->isbn = 0;
        //         $good2->save();
        //     }
        // }

    }

    protected function douban()
    {
        $reg = "/https:\/\/api.douban.com\/v2\/book/";
        $all = BookIsbn::all();
        foreach ($all as $key => $value) {
            if ($value['price'] == 0) {
                dd($value);
                $data = DoubanService::isbnBook($value['isbn13']);
                if (!$data) {
                    Log::error("爬取失败", ['num' => $num, 'isbn' => $good['isbn']]);
                    print "爬取失败!num:" . $num . 'isbn:' . $good['isbn'];
                    return false;
                }
                if (isset($data['code'])) {

                    continue;
                }
                if ($data['price'] == '') {
                    continue;
                }
                $prs = explode(' ', $data['price']);
                if (is_array($prs)) {
                    $value->price = $prs[1];
                }
                $value->price_name = $data['price'];

            } else {
                $value->price_name = $value['price'] . '元';
            }
            $url = preg_replace($reg, "https://book.douban.com/subject", $value['douban_url']);
            $value->douban_url = $url;
            $value->save();
        }
    }
}
